package com.wswy.wzcx.ui.main.home.component

import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.che.libcommon.widget.viewpager.AutoScrollViewPager
import com.che.libcommon.widget.viewpager.InfiniteLoopAdapter
import com.wswy.wzcx.BuildConfig
import com.wswy.wzcx.ui.main.ad.AdItemVo

class HomeAdController (private val adContainer:ViewGroup) {

    companion object {
        private const val TAG = "HomeAdController"

    }

    private var currViewPager:AutoScrollViewPager?=null

    private var currScroll = true

    private var canScroll = false

    fun changeShowAutoScroll(startAutoScroll:Boolean){
        try {
            if (!canScroll){
                return
            }
            currViewPager?.run {
                if (startAutoScroll){
                    if (!currScroll){
                        startAutoScroll()
                        currScroll = true
                    }
                }else{
                    if (currScroll){
                        stopAutoScroll()
                        currScroll = false
                    }
                }
            }
        } catch (e: Throwable) {
        }
    }


    private val context = adContainer.context


    fun showAds(ads:List<AdItemVo>){

        adContainer.removeAllViews()
        canScroll = false
        if (ads.isEmpty()){
            adContainer.visibility = View.GONE
            return
        }

        canScroll = ads.size > 1

        AutoScrollViewPager(context).apply {

            adapter = ADAdapter(ads)
            //offscreenPageLimit = 3
            interval = 2000

            adContainer.visibility = View.VISIBLE

            adContainer.addView(this,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
        }.run {

            //stopAutoScroll()
            if (canScroll){
                startAutoScroll()
            }else{
                stopAutoScroll()
            }
            currScroll = true
            currViewPager =this

        }

    }


    private class ADAdapter(ads:List<AdItemVo>):InfiniteLoopAdapter<AdItemVo>(){


        init {

            setData(ads)
        }

        override fun getItemViewType(position: Int): Int {
            return getItem(position)?.type ?: AdItemVo.SELF_IMG
        }

        override fun getView(position: Int, convertView: View?, container: ViewGroup): View {
            val item = getItem(position)
            val adView = item.adView

            if (BuildConfig.DEBUG){
                Log.e(TAG, " -->$position ")
            }


            (adView.parent as? ViewGroup)?.removeView(adView)

            return when(getItemViewType(position)){

                AdItemVo.SELF_IMG,AdItemVo.GDT_AD,AdItemVo.TTAD -> {
                    (convertView as? ViewGroup ?: FrameLayout(container.context)).apply {
                        removeAllViews()
                        addView(adView)
                    }
                }
                else -> convertView?:FrameLayout(container.context)
            }

        }

    }


}